//
//  YXYNetWorkTool.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/26.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

// 请求方式枚举
enum NetWorkType: String {
    case GET = "GET"
    case POST = "POST"
}

class YXYNetWorkTool: AFHTTPSessionManager {
    
    // 单例
    static var sharedWorkTool: YXYNetWorkTool = {
        let tool = YXYNetWorkTool()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    
    // 请求网络数据
    func loadRequest(type:NetWorkType , urlString:String , parmers:AnyObject? , success: (response: AnyObject?)->() , failure:(error: NSError?)->()) {
        
        SVProgressHUD.show()
        
        // GET请求
        if type == .GET {
            
            self.GET(urlString, parameters: parmers, progress: nil, success: { (_, res) -> Void in
                
                SVProgressHUD.dismiss()
                
                success(response: res)
                
                }, failure: { (_, err) -> Void in
                    
                    SVProgressHUD.showErrorWithStatus(err.description)
                    
                    failure(error: err)
                    
            })
            
        }else {
            
            self.POST(urlString, parameters: parmers, progress: nil, success: { (_, res) -> Void in
                
                SVProgressHUD.dismiss()
                
                success(response: res)
                
                }, failure: { (_, err) -> Void in
                    
                    SVProgressHUD.showErrorWithStatus(err.description)
                    
                    failure(error: err)
                    
            })
            
        }
        
    }
}

extension YXYNetWorkTool {
    
    // 获取userAccount (token等)
    func loadUserAccount(code: String, success:(response:AnyObject?)->(), failure:(error:NSError?)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parmers = [
            "client_id":kAppKey,
            "client_secret":kAppSercet,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":kAppRedirect_uri
        ]
        
        loadRequest(.POST, urlString: urlString, parmers: parmers, success: success, failure: failure)
    }
    
    // 获取用户信息
    func loadUserInfo(userAccount:YXYUserAccount, success:(response:AnyObject?)->(), failure:(error:NSError?)->()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parmers = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        loadRequest(.GET, urlString: urlString, parmers: parmers, success: success, failure: failure)
        
    }
}

extension YXYNetWorkTool {
    
    // 获取微博数据
    func loadStatusData (sinceId: Int64, max_Id: Int64, success:(response: AnyObject?)->(), failure:(error: NSError?)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let parmers = [
            "access_token" : YXYUserAccountTool.sharedUserAccountTool.access_token!,
            "since_id": "\(sinceId)",
            "max_id": "\(max_Id)"
        ]
        
        loadRequest(.GET, urlString: urlString, parmers: parmers, success: success, failure: failure)
    }
    
}

// 发布微博
extension YXYNetWorkTool {
    // 发布文字微博
    func updateTextWeibo (status: String, success:(response: AnyObject?)->(), failure:(error: NSError?)->()) {
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let parmers = [
            "access_token": YXYUserAccountTool.sharedUserAccountTool.access_token!,
            "status": status
        ]
        
        loadRequest(NetWorkType.POST, urlString: urlString, parmers: parmers, success: success, failure: failure)
    }
    
    // 发布图片微博
    func uploadWeibo (status: String, pic: NSData ,success:(response: AnyObject?)->(), failure:(error: NSError?)->()) {
        
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        let parmers = [
            "access_token": YXYUserAccountTool.sharedUserAccountTool.access_token!,
            "status": status,
        ]
        
        POST(urlString, parameters: parmers, constructingBodyWithBlock: { (formData) -> Void in
            
            SVProgressHUD.show()
            
            formData.appendPartWithFileData(pic, name: "pic", fileName: "dage", mimeType: "image/jpeg")
            
            }, progress: nil, success: { (_, JSON) -> Void in
                
                SVProgressHUD.dismiss()
                
                success(response: nil)
                
            }) { (_, error) -> Void in
                
                SVProgressHUD.showErrorWithStatus(error.debugDescription)
                
                failure(error: nil)
        }
        
    }
}
